package org.jeecg.basic.service.impl;

import org.jeecg.basic.entity.MesTestunitheadCfg;
import org.jeecg.basic.entity.MesTestunitdetailCfg;
import org.jeecg.basic.mapper.MesTestunitdetailCfgMapper;
import org.jeecg.basic.mapper.MesTestunitheadCfgMapper;
import org.jeecg.basic.service.IMesTestunitheadCfgService;
import org.jeecg.common.api.CommonAPI;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 产品样件配置
 * @Author: LiCY
 * @Date:   2024-11-27
 * @Version: V1.0
 */
@Service
public class MesTestunitheadCfgServiceImpl extends ServiceImpl<MesTestunitheadCfgMapper, MesTestunitheadCfg> implements IMesTestunitheadCfgService {

	@Autowired
	private MesTestunitheadCfgMapper mesTestunitheadCfgMapper;
	@Autowired
	private MesTestunitdetailCfgMapper mesTestunitdetailCfgMapper;
	@Autowired
	private CommonAPI commonAPI;
	@Override
	public void saveMain(MesTestunitheadCfg mesTestunitheadCfg) {

		String prefix = commonAPI.getPrefix(mesTestunitheadCfg.getLine(), mesTestunitheadCfg.getOp(), mesTestunitheadCfg.getPartno());
		Integer counterValue = commonAPI.getCounterValue("boundary-"+mesTestunitheadCfg.getOp()+"-" + mesTestunitheadCfg.getPartno(),"0");
		//Integer counterValue2 = commonAPI.getCounterValue("recid."+mesTestunitheadCfg.getOp()+"-"+mesTestunitheadCfg.getPartno());
		String format = String.format("%03d", counterValue);
		//生成样件序列号
		mesTestunitheadCfg.setRecId(prefix+format);
		mesTestunitheadCfgMapper.insert(mesTestunitheadCfg);
//		if(mesTestunitdetailCfgList!=null && mesTestunitdetailCfgList.size()>0) {
//			for(MesTestunitdetailCfg entity:mesTestunitdetailCfgList) {
//				entity.setHeadId(mesTestunitheadCfg.getRecId());
//				mesTestunitdetailCfgMapper.insert(entity);
//			}
//		}
	}

	@Override
	public void updateMain(MesTestunitheadCfg mesTestunitheadCfg,List<MesTestunitdetailCfg> mesTestunitdetailCfgList) {
		mesTestunitheadCfgMapper.updateById(mesTestunitheadCfg);
		
		//1.先删除子表数据
		mesTestunitdetailCfgMapper.deleteByMainId(mesTestunitheadCfg.getRecId());
		
		//2.子表数据重新插入
		if(mesTestunitdetailCfgList!=null && mesTestunitdetailCfgList.size()>0) {
			for(MesTestunitdetailCfg entity:mesTestunitdetailCfgList) {
				//外键设置
				entity.setHeadId(mesTestunitheadCfg.getRecId());
				mesTestunitdetailCfgMapper.insert(entity);
			}
		}
	}

	@Override
	public void delMain(String id) {
		mesTestunitdetailCfgMapper.deleteByMainId(id);
		mesTestunitheadCfgMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			mesTestunitdetailCfgMapper.deleteByMainId(id.toString());
			mesTestunitheadCfgMapper.deleteById(id);
		}
	}
	
}
